package com.kxmall.web.controller.rider.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kxmall.common.core.domain.model.LoginUser;
import com.kxmall.common.enums.DeviceType;
import com.kxmall.common.enums.RiderStatusType;
import com.kxmall.common.enums.RiderWorkStateType;
import com.kxmall.common.enums.UserType;
import com.kxmall.common.exception.ServiceException;
import com.kxmall.common.helper.LoginHelper;
import com.kxmall.huishou.domain.HsRecoveryVehicle;
import com.kxmall.huishou.mapper.HsRecoveryVehicleMapper;
import com.kxmall.rider.domain.KxRider;
import com.kxmall.rider.domain.vo.KxRiderOrderVo;
import com.kxmall.rider.domain.vo.RiderLoginDTO;
import com.kxmall.rider.mapper.KxRiderMapper;
import com.kxmall.web.controller.rider.service.RiderInfoService;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @description:
 * @author: kxmall
 * @date: 2020/02/28 11:29
 **/
@Service
public class RiderInfoServiceImpl implements RiderInfoService {

    @Resource
    private KxRiderMapper riderMapper;

    @Resource
    private HsRecoveryVehicleMapper recoveryVehicleMapper;

    @Resource
    private StringRedisTemplate userRedisTemplate;

    @Override
    public RiderLoginDTO loginRider(String phone, String password, String ip) {
        String cryptPassword = Md5Crypt.md5Crypt(password.getBytes(), "$1$" + phone.substring(0, 7));
        KxRider riderDO = new KxRider();
        riderDO.setPhone(phone);
        riderDO.setPassword(cryptPassword);
        KxRider kxRider = riderMapper.selectOne(new LambdaQueryWrapper<>(riderDO));
        if (kxRider == null) {
            throw new ServiceException("密码错误!");
        }
        if (kxRider.getState() == RiderStatusType.ABORT.getCode()) {
            throw new ServiceException("骑手处于禁用状态，请联系管理员");
        }
        RiderLoginDTO riderLoginDTO = new RiderLoginDTO();
        BeanUtils.copyProperties(kxRider, riderLoginDTO);

        // 此处可根据登录用户的数据不同 自行创建 loginUser
        LoginUser loginUser = buildLoginUser(kxRider);
        // 生成token
        LoginHelper.loginByDevice(loginUser, DeviceType.RIDER);
        riderLoginDTO.setAccessToken(StpUtil.getTokenValue());
        return riderLoginDTO;
    }

    private LoginUser buildLoginUser(KxRider kxRider) {
        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(kxRider.getId());
        loginUser.setUsername(kxRider.getName());
        loginUser.setUserType(UserType.RIDER_USER.getUserType());
        loginUser.setOpenId(kxRider.getOpenId());
        loginUser.setLoginType(kxRider.getLoginType());
        return loginUser;
    }

    @Override
    public RiderLoginDTO fixPass(String oldPassword, String newPassword, String ip, Long riderId) throws ServiceException {
        return null;
    }

    @Override
    public String toWorking(Long riderId) throws ServiceException {
        KxRider riderDO = new KxRider();
        riderDO.setId(riderId);
        riderDO.setWorkState(RiderWorkStateType.WORKING.getCode());
        if (riderMapper.updateById(riderDO) > 0) {
            return "ok";
        }
        throw new ServiceException("配送员信息更新异常");
    }

    @Override
    public String toRest(Long riderId) throws ServiceException {
        KxRider riderDO = new KxRider();
        riderDO.setId(riderId);
        riderDO.setWorkState(RiderWorkStateType.REST.getCode());
        if (riderMapper.updateById(riderDO) > 0) {
            return "ok";
        }
        throw new ServiceException("配送员信息更新异常");
    }

    @Override
    public RiderLoginDTO getWorkingState(Long riderId) throws ServiceException {
        KxRider riderDO = riderMapper.selectById(riderId);
        if (riderDO != null) {
            RiderLoginDTO riderLoginDTO = new RiderLoginDTO();
            BeanUtils.copyProperties(riderDO, riderLoginDTO);
            return riderLoginDTO;
        }
        throw new ServiceException("配送员不存在");
    }

    @Override
    public String updateAddress(KxRiderOrderVo riderOrderVo) {
        Long riderId = riderOrderVo.getRiderId();
        BigDecimal longitude = riderOrderVo.getLongitude();
        BigDecimal latitude = riderOrderVo.getLatitude();
        LambdaQueryWrapper<HsRecoveryVehicle> q = new LambdaQueryWrapper<>();
        q.eq(HsRecoveryVehicle::getRiderId,riderId);
        q.eq(HsRecoveryVehicle::getStatus,1);
        List<HsRecoveryVehicle> vehicleList = recoveryVehicleMapper.selectList(q);
        if(vehicleList != null && vehicleList.size() != 0){
            for (HsRecoveryVehicle hsRecoveryVehicle : vehicleList) {
                hsRecoveryVehicle.setLongitude(longitude);
                hsRecoveryVehicle.setLatitude(latitude);
            }
            recoveryVehicleMapper.updateBatchById(vehicleList);
        }
        return "ok";
    }

}
